package com.ruisoft.business.stock.controller;

import com.ruisoft.base.controller.BaseController;
import com.ruisoft.business.stock.service.StockService;
import com.ruisoft.common.Constants;
import com.ruisoft.core.json.JSONMap;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: LFC
 * Date: 2015/8/11
 * Time: 21:38
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/stock/")
public class StockCroller extends BaseController{

    private static final Logger LOG = Logger.getLogger(StockCroller.class);
     @Autowired
     public StockService stockService;
    //新增默认初始数据
     public void getValue(){
         JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
         //公司
         json.put("company_id","");
         //部门
         json.put("dept_id","");
         //制单人
         json.put("create_uid","");
         // 单据状态
         json.put("stats","");
     }

    /**
     * 分页查找库存信息
     * @throws Exception
     */
    @RequestMapping("/searchPageStock")
    public void searchPageStock() throws Exception {
        JSONObject reqData = getReqData();
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        List<JSONObject> result = stockService.searchPage(reqData, "stock.select.stock");
        json.put("data", result);
        json.put("status",1);
        response(json);
    }

    /**
     *根据库存分拣单ID，查询单条分拣单信息
     * @param id  库存分拣单ID
     * @throws Exception
     */
    @RequestMapping("searchStock/{id}")
    public void searchStock(@PathVariable String id) throws Exception{
        JSONObject result = stockService.searchStock(id);
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        json.put("data", result);
        json.put("status",1);
        response(json);
    }

    /**
     * 保存库存信息
     */
    @RequestMapping("saveStock")
    public void saveStock(){
        try {
            JSONObject reqData = getReqData();
            JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
            JSONObject result = new JSONObject();
            int status = 0;
            result = stockService.saveStock(reqData);
            json.put("data", result);
            json.put("status", status);
            response(json);
        } catch (Exception e) {
            LOG.error("新增库存业务发生错误", e);
        }
    }

    /**
     * 更新库存信息
     */
    @RequestMapping("updateStock")
    public void updateStock(){
        try {
            JSONObject reqData = getReqData();
            JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
            JSONObject result = new JSONObject();
            int status = 0;
            result = stockService.updateStock(reqData);
            json.put("data", result);
            json.put("status", status);
            response(json);
        } catch (Exception e) {
            LOG.error("修改库存业务发生错误", e);
        }
    }

    /**
     * 更改库存分拣单状态
     */
    @RequestMapping("updateReceipt")
    public void updateReceipt(){
        try {
            JSONObject reqData = getReqData();
            JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
            JSONObject result = new JSONObject();
            int status = 0;
            result = stockService.updateDetail(reqData,"stock.update.stock_picking_state","stock.update.stock_picking_move_state");
            json.put("data", result);
            json.put("status", status);
            response(json);
        } catch (Exception e) {
            LOG.error("修改库存状态发生错误", e);
        }
    }

    /**
     * 确认收货，更新实际库存
     */
    @RequestMapping("saveStockQuanty")
    public void saveStockQuanty(){
        try {
            JSONObject reqData = getReqData();
            JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
            JSONObject result = new JSONObject();
            int status = 0;
            result = stockService.updateQuant(reqData);
            json.put("data", result);
            json.put("status", status);
            response(json);
        } catch (Exception e) {
            LOG.error("新增库存业务发生错误", e);
        }
    }

    /**
     * 删除库存分拣单
     * @param id 分拣单ID
     * @throws Exception
     */
    @RequestMapping(value = "deleteStock/{id}", method = RequestMethod.GET)
    public void deleteStock(@PathVariable String id) throws Exception {
        int status = 1;
        int r = stockService.deleteDetail(id, "stock.delete.stock_picking", "stock.delete.stock_picking_move");
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        if(r < 0){
            status = -1;
        }
        json.put("status", status);
        response(json);
    }

    /**
     * 删除库存移动明细数据
     * @param id 库存移动ID
     * @throws Exception
     */
    @RequestMapping(value = "deleteDictItem/{id}", method = RequestMethod.GET)
    public void deleteStockItem(@PathVariable String id) throws Exception {
        int status = 1;
        int r = stockService.deletecode(id, "stock.delete.stock_move");
        if(r < 0){
            status = -1;
        }
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        json.put("status", status);
        response(json);
    }
}
